package name.bingo.pay.simulation;
/**
 * create by yanbing on 2018/11/8 15:46
 */

import name.bingo.bing.pay.utils.IdGen;
import name.bingo.utils.SimpleHttpUtils;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.lang.math.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @param:
 * @return:
 * @auther: yanbing
 * @date: 2018/11/8 15:46
 **/
public class MainTest {
    public  static Logger logger=LoggerFactory.getLogger(MainTest.class);
    public static void main(String[] args) {
        logger.info("进入模拟测试下单方法");
        final Map<String , Object> mayun=new HashMap<String , Object>();
        mayun.put("userId","10001");mayun.put("orderAmount",new BigDecimal(10));
        final Map<String , Object> maht=new HashMap<String , Object>();
        maht.put("userId","10002");maht.put("orderAmount",new BigDecimal(20));
        final Map<String , Object> liyh=new HashMap<String , Object>();
        liyh.put("userId","10003");liyh.put("orderAmount",new BigDecimal(50.6));
        final Map<String , Object> dinglei=new HashMap<String , Object>();
        dinglei.put("userId","10004");dinglei.put("orderAmount",new BigDecimal(100.0));
        final Map<String , Object> zhouhy=new HashMap<String , Object>();
        zhouhy.put("userId","10005");zhouhy.put("orderAmount",new BigDecimal(200.0));
        new Thread(new Runnable() {
            public void run() {
                testGateWay(mayun);
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                testGateWay(maht);
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                testGateWay(liyh);
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                testGateWay(dinglei);
            }
        }).start();
        new Thread(new Runnable() {
            public void run() {
                testGateWay(zhouhy);
            }
        }).start();

    }
    private static void testGateWay(Map<String,Object> user){
        String datajson=JSONObject.fromObject(user).toString();
        for(int i=0;i<100;i++){
            try {
                int random = RandomUtils.nextInt(10);
                long sleepNum  = 10L * random;
                Thread.sleep(sleepNum);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //创建订单
            Map orderMap = SimpleHttpUtils.doPostJson("http://127.0.0.1:8080/pay/initOrderPay",datajson);
            if(!"200".equals(orderMap.get("status"))){
                continue;
            }
            System.out.println("订单号："+orderMap.get("msg"));
            // 用户支付行为模拟（等待片刻）
            try {
                Thread.sleep(200L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //模拟构建银行扣款成功结果通知
            Map<String , Object> notifyMap = getNotifyRequestMap((String)orderMap.get("msg"),user);
            Map notifyReturnMap = SimpleHttpUtils.doPostJson("http://127.0.0.1:8080/pay/notify", JSONObject.fromObject(notifyMap).toString());
            System.out.println(notifyReturnMap.toString());
        }

    }
    /**
     * 模拟构建银行扣款成功结果通知
     * @param
     * @return
     */
    private static Map<String , Object> getNotifyRequestMap(String orderId,Map<String,Object> user){
        Map<String, Object> notifyMap = new HashMap<String, Object>();
        notifyMap.put("result_code", "SUCCESS");
        String timeEnd = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
        notifyMap.put("time_end", timeEnd);
        notifyMap.put("out_trade_no", orderId);
        notifyMap.put("transaction_id", IdGen.idGen());
        notifyMap.put("total_amount",user.get("orderAmount"));

        return notifyMap;
    }
}
